package com.xiashitech.agent.proxy.hutool;

import com.xiashitech.agent.cache.classloader.ClassLoaderCache;
import com.xiashitech.agent.config.StartConfig;

public class Setting {

    private static String className = "cn.hutool.setting.Setting";

    private Object setting;

    public static Setting getInstance(String path) {
        try {
            Class settingClass = ClassLoaderCache.getLaunchClassLoader().loadClass(className);
            Setting settingInstance = new Setting();
            settingInstance.setting = settingClass.getDeclaredConstructor(String.class).newInstance(path);
            return settingInstance;
        } catch (Throwable e) {
            StartConfig.throwableStackPrinter(e);
        }
        return null;
    }

    public void set(String key, String value) {
        try {
            if (setting == null) {
                return;
            }
            Class settingClass = ClassLoaderCache.getLaunchClassLoader().loadClass(className);
            settingClass.getMethod("set", String.class, String.class).invoke(setting,key,value);
        } catch (Throwable e) {
            StartConfig.throwableStackPrinter(e);
        }
    }

    public Object getValue() {
        return setting;
    }
}
